4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
17 namespace Microsoft
.JScript
{
20 using System
.Reflection
;
21 using System
.Reflection
.Emit
;
22 using System
.Globalization
;
23 using System
.Diagnostics
;
25 internal sealed class JSWrappedMethod
: JSMethod
, IWrappedMember
{
26 internal MethodInfo method
;
27 private ParameterInfo
[] pars
;
29 internal JSWrappedMethod(MethodInfo method
, Object obj
)
32 if (method
is JSMethodInfo
) method
= ((JSMethodInfo
)method
).method
;
33 this.method
= method
.GetBaseDefinition();
34 this.pars
= this.method
.GetParameters();
35 if (obj
is JSObject
&& !Typeob
.JSObject
.IsAssignableFrom(method
.DeclaringType
))
36 if (obj
is BooleanObject
) this.obj
= ((BooleanObject
)obj
).value;
37 else if (obj
is NumberObject
) this.obj
= ((NumberObject
)obj
).value;
38 else if (obj
is StringObject
) this.obj
= ((StringObject
)obj
).value;
39 else if (obj
is ArrayWrapper
) this.obj
= ((ArrayWrapper
)obj
).value;
42 public override MethodAttributes Attributes
{
44 return this.method
.Attributes
;
48 private Object
[] CheckArguments(Object
[] args
){
49 Object
[] newArgs
= args
;
50 if (args
!= null && args
.Length
< this.pars
.Length
){
51 newArgs
= new Object
[this.pars
.Length
];
52 ArrayObject
.Copy(args
, newArgs
, args
.Length
);
53 for (int i
= args
.Length
, size
= this.pars
.Length
; i
< size
; i
++)
54 newArgs
[i
] = System
.Type
.Missing
; // this will take care of the default value
59 internal override Object
Construct(Object
[] args
){
60 if (this.method
is JSMethod
)
61 return ((JSMethod
)(this.method
)).Construct(args
);
62 if (this.method
.GetParameters().Length
== 0 && this.method
.ReturnType
== Typeob
.Object
){
63 Object func
= this.method
.Invoke(this.obj
, BindingFlags
.SuppressChangeType
, null, null, null);
64 if (func
is ScriptFunction
)
65 return ((ScriptFunction
)func
).Construct(args
);
67 throw new JScriptException(JSError
.NoConstructor
);
70 public override Type DeclaringType
{
72 return this.method
.DeclaringType
;
76 internal override String
GetClassFullName(){
77 if (this.method
is JSMethod
)
78 return ((JSMethod
)this.method
).GetClassFullName();
80 return this.method
.DeclaringType
.FullName
;
83 internal override PackageScope
GetPackage(){
84 if (this.method
is JSMethod
){
85 return ((JSMethod
)this.method
).GetPackage();
90 public override ParameterInfo
[] GetParameters(){
94 internal override MethodInfo
GetMethodInfo(CompilerGlobals compilerGlobals
){
95 if (this.method
is JSMethod
)
96 return ((JSMethod
)(this.method
)).GetMethodInfo(compilerGlobals
);
101 public Object
GetWrappedObject(){
106 [DebuggerStepThroughAttribute
]
107 [DebuggerHiddenAttribute
]
109 public override Object
Invoke(Object obj
, BindingFlags options
, Binder binder
, Object
[] parameters
, CultureInfo culture
){
110 parameters
= CheckArguments(parameters
);
111 return this.Invoke(this.obj
, this.obj
, options
, binder
, parameters
, culture
);
115 [DebuggerStepThroughAttribute
]
116 [DebuggerHiddenAttribute
]
118 internal override Object
Invoke(Object obj
, Object thisob
, BindingFlags options
, Binder binder
, Object
[] parameters
, CultureInfo culture
){
119 parameters
= CheckArguments(parameters
);
120 if (this.obj
!= null && !(this.obj
is Type
)) obj
= this.obj
;
121 if (this.method
is JSMethod
)
122 return ((JSMethod
)this.method
).Invoke(obj
, thisob
, options
, binder
, parameters
, culture
);
124 return this.method
.Invoke(obj
, options
, binder
, parameters
, culture
);
127 public override String Name
{
129 return this.method
.Name
;
133 public override Type ReturnType
{
135 return this.method
.ReturnType
;
139 public override String
ToString(){
140 return this.method
.ToString();